/*
* This file is part of washmatique-lib.

* washmatique-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this library;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

* Copyright (C) 2011 boris Sabadach
* http://washmatique.fr/

*/
package fr.washmatique.spark.components.navigators {

	import flash.events.Event;
	import flash.events.MouseEvent;

	import fr.washmatique.spark.components.ISelectableVisualElement;

	import mx.events.FlexEvent;

	import spark.components.Button;


	/**
	 * Defines a component which purpose is to be used as the header of an AccordionElement
	 * @see fr.washmatique.spark.components.Accordion
	 * @see fr.washmatique.spark.components.AccordionElement
	 */
	public class AccordionElementHeader extends Button implements ISelectableVisualElement {

		private var _selected:Boolean=false;

		private var _switchAble:Boolean=false;


		public function AccordionElementHeader() {
			super();
		}

		public function get switchAble():Boolean {
			return _switchAble;
		}

		/**
		 * a flag that tells if this button can act as an on/off button.
		 * @param value
		 *
		 */
		public function set switchAble(value:Boolean):void {
			if (value == _switchAble)
				return;
			_switchAble=value;

		}


		public function set selected(value:Boolean):void {
			if (value == _selected) {
				return;
			}

			_selected=value;
			dispatchEvent(new FlexEvent(FlexEvent.VALUE_COMMIT));
			invalidateSkinState();

		}

		[Bindable("valueCommit")]
		[Bindable("change")]
		/**
		 * a flag that tells if this button is selected.
		 * If setting this property has a mouse click origin, a change event and value commit event will be dispatched, while setting this property programmatically will only dispatch a value commit event.
		 * @return
		 *
		 */
		public function get selected():Boolean {
			return _selected;
		}


		//--------------------------------------------------------------------------
		//
		//  Overridden methods
		//
		//--------------------------------------------------------------------------


		override protected function getCurrentSkinState():String {
			if (!enabled) {
				return super.getCurrentSkinState();
			}
			return _selected ? "down" : super.getCurrentSkinState();
		}


		override protected function clickHandler(event:MouseEvent):void {
			super.clickHandler(event);
			if (!_switchAble && !selected) {
				selected=true;
				dispatchEvent(new Event(Event.CHANGE));
			} else if (_switchAble) {
				dispatchEvent(new Event(Event.CHANGE));
				selected=!selected;
			}

		}

	}
}

